﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

namespace HPT.Utilities
{
    //Extension methods must be defined in a static class 
    public static class StringExtension
    {
        // This is the extension method. 
        // The first parameter takes the "this" modifier
        // and specifies the type for which the method is defined. 
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }

        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }

    }
    //Extension methods must be defined in a static class 
    public static class BooleanExtension
    {
        // This is the extension method. 
        // The first parameter takes the "this" modifier
        // and specifies the type for which the method is defined. 
        public static string ToYesNo(this Boolean val)
        {
            return val?"Yes":"No";
        }

        public static string ToYesNo(this Boolean? val)
        {
            if (val == null) return string.Empty;
            else
            return val.Value.ToYesNo();
        }
    }

    public static class OExtension
    {
        public static SPFieldLookupValueCollection ToSPLookUpCollection<T>(this List<T> list)
        {
            var result = new SPFieldLookupValueCollection();
            foreach (var item in list)
                result.Add((item as SPItem).ToLookupValue());
            return result;
        }
    }

    
    public partial class OConvert
    {
        public static DateTime? ToDateTime(object obj)
        {
            try
            {
                DateTime? result = null;
                if (obj != null)
                    result = Convert.ToDateTime(obj);
                return result;
            }
            catch { return null; }
        }
        public static bool IsEmail(string emailInput)
        {
            return ORegex.IsEmail(emailInput);
        }
        public static bool IsDateTime(string dateInput)
        {
            try
            {
                DateTime.Parse(dateInput);
                return true;
            }
            catch { return false; }
        }
        public static bool IsDateTime(string dateInput, string dateFormat)
        {
            try
            {
                DateTime.ParseExact(dateInput, dateFormat, null);
                return true;
            }
            catch { return false; }
        }
        public static bool IsTime(string timeInput)
        {
            return ORegex.IsTime(timeInput);
        }
        public static bool CompareTimeGreaterThan(string time1, string time2, string timeFormat)
        {
            try
            {
                DateTime dt1 = ToDateTime(time1, timeFormat).Value;
                DateTime dt2 = ToDateTime(time2, timeFormat).Value;
                return dt1 < dt2;
            }
            catch { return false; }
        }
        public static bool ValueIs(string value, params string[] values)
        {
            return values.Contains(value);
        }
        public static DateTime? ToDateTime(string strDateTime, string DATE_FORMAT)
        {
            try
            {
                if (String.IsNullOrEmpty(strDateTime))
                    return null;
                else
                    return DateTime.ParseExact(strDateTime, DATE_FORMAT, null);
            }
            catch { return null; }
        }
        public static bool IsDecimal(string decimalNumber)
        {
            //if (decimalNumber.Contains(","))
            //    decimalNumber = decimalNumber.Replace(',', '.');
            decimal d; return Decimal.TryParse(decimalNumber, out d);
        }
        public static bool IsInt(string number)
        {
            int i; return int.TryParse(number, out i);
        }
        //public static bool IsCorrectDecimalFormat(string decimalNumber)
        //{
        //    if (decimalNumber.Contains(","))
        //        decimalNumber = decimalNumber.Replace(',', '.');
        //    string tailer = "";
        //    if (decimalNumber.Contains("."))
        //        tailer = decimalNumber.Substring(decimalNumber.LastIndexOf('.'));
        //    return tailer.Length <= 3;
        //}
        public static int CompareString(string s1, string s2, bool asc)
        {
            if (String.IsNullOrEmpty(s1)) s1 = "";
            if (String.IsNullOrEmpty(s2)) s2 = "";
            if (asc) return s1.CompareTo(s2);
            else return s2.CompareTo(s1);
        }
        public static int CompareDateTime(DateTime? d1, DateTime? d2, bool asc)
        {
            if (d1 == null) d1 = new DateTime();
            if (d2 == null) d2 = new DateTime();
            if (asc) return d1.Value.CompareTo(d2.Value);
            else return d2.Value.CompareTo(d1.Value);
        }
        public static Decimal ToDecimal(object decimalNumber)
        {
            return ToDecimal(ToString(decimalNumber));
        }
        public static Decimal ToDecimal(string decimalNumber)
        {
            //if (decimalNumber.Contains(","))
            //    decimalNumber = decimalNumber.Replace(',', '.');
            if (IsDecimal(decimalNumber))
                return Decimal.Parse(decimalNumber);
            else return 0;
        }

        public static decimal ToPercent(string value)
        {
            decimal percent = 0;
            if (value.Contains("%"))
                percent = ToDecimal(value.Replace("%", "").Trim()) / 100M;
            else
                percent = ToDecimal(value);
            return percent;
        }

        public static int ToInt(string number)
        {
            if (number == null) return 0;
            if (number.Contains(",") || number.Contains(".")) number = number.Replace(",", "").Replace(".", "");
            if (IsInt(number))
                return int.Parse(number);
            else return 0;
        }
        public static int ToInt(object number)
        {
            if (number != null && IsInt(number.ToString()))
                return int.Parse(number.ToString());
            else return 0;
        }
        public static long ToLong(object number)
        {
            try
            {
                return number != null ? long.Parse(number.ToString()) : 0;
            }
            catch
            {
                return 0;
            }
        }


        public static void SetCheck(bool? result, RadioButton rad1, RadioButton rad2)
        {
            if (result == null)
                return;
            else
            {
                if (result.Value == true)
                    rad1.Checked = true;
                else
                    rad2.Checked = true;
            }
        }

        public static bool? GetCheckValue(RadioButton rad1, RadioButton rad2)
        {
            if (!rad1.Checked && !rad2.Checked)
            {
                return null;
            }
            else
            {
                return rad1.Checked;
            }
        }



        public static string ToStringWithComma(string[] arr)
        {
            StringBuilder sb = new StringBuilder();
            if (arr != null && arr.Count() > 0)
            {
                foreach (string s in arr)
                {
                    if (sb.Length > 0) sb.AppendFormat(", {0}", s);
                    else sb.Append(s);
                }
            }
            return sb.ToString();
        }



   
        public static string ToStringWithDateTime(DateTime? obj, string dateFormat)
        {
            return obj == null ? "" : obj.Value.ToString(dateFormat);
        }
        public static string AttachmentsDateTimeToString(DateTime dateTime) //[Return] at 23:00 on June 21th
        {
            return String.Format("at {0} on {1} {2}", dateTime.ToString("hh:mm"), dateTime.ToString("MMMM"), DateSubfix(dateTime.Day));
        }
        public static string DateSubfix(int day)
        {
            string subfix = "th";
            if (day == 1 || day == 21 || day == 31) subfix = "st";
            if (day == 2 || day == 22) subfix = "nd";
            if (day == 3 || day == 23) subfix = "rd";
            return day.ToString() + subfix;
        }
        public static string ToString(object obj)
        {
            return obj == null ? "" : obj.ToString();
        }
        public static string ToStringWithTrim(object obj)
        {
            return obj == null ? "" : obj.ToString().Trim();
        }
        public static SPFieldLookupValue ToLookupValue(object obj)
        {
            return obj == null ? null : new SPFieldLookupValue(ToString(obj));
        }
        public static SPFieldLookupValueCollection ToLookupValueCollection(object obj)
        {
            return obj == null ? null : new SPFieldLookupValueCollection(ToString(obj));
        }
        public static string ReplaceStringSetValue(string value)
        {
            return value.Replace("\n", "").Replace("\r", "").Replace("<", "&lt;").Replace(">", "&gt;");
        }
        public static string ReplaceStringGetValue(string value)
        {
            return value.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"");
        }
        public static bool ToBoolean(object obj)
        {
            try
            {
                return obj == null ? false : bool.Parse(obj.ToString());
            }
            catch
            {
                return obj == null ? false : "1".Equals(obj.ToString());
            }
        }

        public static bool? ToBooleanWithNull(object obj)
        {
            return obj == null ? (bool?)null : bool.Parse(obj.ToString());
        }

        public const string ICON_WORD = "/_layouts/images/ICDOCX.GIF";
        public const string ICON_EXCEL = "/_layouts/images/ICXLSX.PNG";
        public const string ICON_IMAGE = "/_layouts/images/ICBMP.GIF";
        public const string ICON_PDF = "/_layouts/images/ITDECIS.PNG";
        public const string ICON_OTHER = "/_layouts/images/ITDECIS.PNG";
        public static string GetIconUrl(string docIcon)
        {
            docIcon = docIcon.ToLower();
            if (ValueIs(docIcon, "doc", "docx")) { return ICON_WORD; }
            else if (ValueIs(docIcon, "xls", "xlsx", "csv")) { return ICON_EXCEL; }
            else if (ValueIs(docIcon, "bmp", "gif", "jpg", "png", "psd", "tif")) { return ICON_IMAGE; }
            else if (ValueIs(docIcon, "pdf")) { return ICON_PDF; }
            else return ICON_OTHER;
        }

        public static SPUser ToSPUser(object obj)
        {
            SPFieldUserValue user = ToSPFieldUserValue(obj);
            if (user == null) return null;
            try
            {
                return user.User;
            }
            catch { return null; }
        }

        public static SPFieldUserValue ToSPFieldUserValue(object obj)
        {
            if(obj == null) return null;
            try
            {
                SPFieldUserValue lookupValue = new SPFieldUserValue(OWeb.CurrentWeb, ToString(obj));
                return lookupValue;
            }
            catch { return null; }
        }

        public static SPFieldUserValueCollection ToSPFieldUserValueCollection(object obj)
        {
            if(obj == null) return null;
            try
            {
                SPFieldUserValueCollection lookupValue = new SPFieldUserValueCollection(OWeb.CurrentWeb, ToString(obj));
                return lookupValue;
            }
            catch { return null; }
        }
        private static readonly string[] XMLSpecialChars = new string[] { "&", "<", ">", "\"", "'" };
        private static readonly string[] XMLSpecialCharsReplace = new string[] { "&amp;", "&lt;", "&gt;", "&quot;", "&apos;" };
        public static string ReplaceXMLSpecialChars(string value)
        {
            for (int i = 0; i < XMLSpecialChars.Count(); i++ )
                if(value.Contains(XMLSpecialChars[i]))
                    value = value.Replace(XMLSpecialChars[i], XMLSpecialCharsReplace[i]);
            return value;
        }

        public static string CorrectData(string Poems)
        {
            Regex regex = new Regex(@"(\r\n|\r|\n)+"); return regex.Replace(Poems, "<br />").Replace("'", @"\'");
        }

        private static readonly char[] FileNameSpecialChars = new char[] { '/', '\\', '?', '%', '*', ':', '|', '"', '<', '>' };
        public static string CorrectFileName(string fileName)
        {
            //if (fileName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
            //    foreach (char specialChars in System.IO.Path.GetInvalidFileNameChars())
            //        fileName = fileName.Replace(specialChars, '_');
            if (fileName.IndexOfAny(FileNameSpecialChars) != -1)
                foreach (char specialChars in FileNameSpecialChars)
                    fileName = fileName.Replace(specialChars, '_');
            return fileName;
        }

        //public static string ReplaceHTML(string html)
        //{
        //    if (!html.Contains("</table>")) return html;

        //    return html
        //        .Replace("<table", "<div").Replace("</table>", "</div>")
        //        .Replace("<tr", "<div").Replace("</tr>", "</div>")
        //        .Replace("<td", "<span").Replace("</td>", "</span>")
        //        .Replace("<tbody>", "").Replace("</tbody>", "");
        //        //.Replace("<img alt=\"\" src=\"/_layouts/styles/WebALCAutoReport/images/arrow-orng-wht-se.gif\">", "<span style=\"color:#FF6600;font-family:Arial\">&gt</span> "); //▪ <span style=\"color:#FF6600;font-size:8px;font-family:Wingdings\">î</span>
        //}

        public static string StripHTML(string HTMLText)
        {
            Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
            return reg.Replace(HTMLText, "");
        }

        public static string GetShortDetail(string contents, int lenght)
        {
            contents = StripHTML(contents);
            if (contents.Length > lenght)
            {
                if (contents[lenght].Equals(' '))
                {
                    contents = contents.Remove(lenght);
                    contents += " ...";
                }
                else
                {
                    contents = contents.Remove(lenght);
                    int iLastSpacePos = contents.LastIndexOf(' ');
                    if (iLastSpacePos > 0)
                    {
                        contents = contents.Remove(iLastSpacePos);
                    }
                    contents += " ...";
                }
            }
            return contents;
        }

        public static string GetFirstImage(string contents)
        {
            string result = "";
            Regex rg = new Regex(@"<img.*?src=""(.*?)""", RegexOptions.IgnoreCase);
            foreach (Match m in rg.Matches(contents))
            {
                result = m.Groups[1].Value;
                break;
            }
            return result;
        }  

        public static string ToYesNo(bool value)
        {
            return value ? "Yes" : "No";
        }

        public static string[] ToArray(IEnumerable<string> values)
        {
            return values == null ? null : values.ToArray();
        }

        internal static string ToStringWithComma(IEnumerable<int> arr)
        {
            StringBuilder sb = new StringBuilder();
            if (arr != null && arr.Count() > 0)
            {
                foreach (int s in arr)
                {
                    if (sb.Length > 0) sb.AppendFormat(",{0}", s);
                    else sb.Append(s);
                }
            }
            return sb.ToString();
        }

        public static string CorrectHyperLink(string link)
        {
            if (!String.IsNullOrEmpty(link) && !link.Contains(@"//")) link = string.Format(@"http://{0}", link);
            return link;
        }
    }
}